package com.ssm.core.mq;

import com.rabbitmq.client.Channel;
import com.ssm.common.constant.Constants;
import com.ssm.common.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class AMQPService {
    // @RabbitListener(queues = "test.abc")
    public void receive2(Object object) {
        System.out.println(object);
    }

    @RabbitListener(queues = Constants.MQ_DATA_QUEUE)
    public void receive(@Payload Object mqData, Message message, Channel channel) throws IOException {
        // 处理业务逻辑

        /* 采用手动应答模式, 手动确认应答更为安全稳定
         * 取值为 false 时，表示通知 RabbitMQ 当前消息被确认
         * 如果为 true，则额外将比第一个参数指定的 delivery tag 小的消息一并确认
         */
        log.info("MQ_DATA_QUEUE RECEIVE:" + JsonUtil.obj2String(mqData));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}